package com.newland.boss1.zxy.monitor;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author : zhangxuyuan
 * @Project: springbootprometheus
 * @Package com.newland.boss1.zxy.monitor
 * @date Date : 2021年03月15日 9:30
 */
@Component
@Aspect
public class CounterMonitor {

    @Autowired
    MeterRegistry meterRegistry;


    @Pointcut("@annotation(com.newland.boss1.zxy.monitor.CounterMetric)")
    public void pointcut() {}

    @Around("pointcut()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable
    {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        CounterMetric annotation = method.getAnnotation(CounterMetric.class);
        System.out.println(annotation.name());
        Counter counter=Counter.builder(annotation.name()).description(annotation.description())
                .tags(annotation.tag()).register(meterRegistry);

        counter.increment();
        System.out.println(counter.count());
        return joinPoint.proceed();
    }
}
